package com.lianniu.web.seller.service.impl;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.product.model.Page;
import com.lianniu.web.product.model.SkuProductVo;
import com.lianniu.web.seller.dao.ISupplyProductDao;
import com.lianniu.web.seller.service.ISupplyProductService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-05-09.
 */
@Service
public class SupplyProductServiceImpl implements ISupplyProductService {
    @Autowired
    ISupplyProductDao supplyProductDao;
    Logger logger = LoggerFactory.getLogger(SupplyProductServiceImpl.class);
    @Override
    public BaseResult getSupplyNum() {
        int num = supplyProductDao.supplyNum();
        return BaseResult.success(num);
    }

    @Override
    public BaseResult getList(Integer pageNum,String userId) {
        logger.info("ProductServiceImpl getList begin");
        Map<String, Object> param = new HashMap<>();
        int count = 0;
        Page page = null;

        try {
            count = supplyProductDao.selectMyProductCount(param);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }
        if (StringUtils.isBlank(userId) || "null".equals(userId)){
            count = 3;
        }
        page = new Page(count, pageNum);
        param.put("page", page);

        List<SkuProductVo> list = null;

        try {
            if (pageNum <= page.getPageSum()) {
                list = supplyProductDao.getList(param);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }

        logger.info("ProductServiceImpl getList end");
        return BaseResult.success(list);
    }
}
